package com.example.highway.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.highway.config.MyJson;
import com.example.highway.dao.InspectorDao;
import com.example.highway.dao.OwnerDao;
import com.example.highway.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

@RestController
@CrossOrigin
@RequestMapping("owner")
public class OwnerController {
    @Autowired
    OwnerDao ownerDao;

    @RequestMapping("add")
    public MyJson add(@RequestBody() String msg){
//        JSONObject jsonObject = JSON.parseObject(msg);
//
//        Optional<Owner> o = ownerDao.findById((String)jsonObject.get("id"));
//        if(o.isPresent()){
//            return new MyJson("0", false);
//        }
//
//        Owner c = new Owner();
//        c.setId((String)jsonObject.get("id"));
//        c.setPassword((String)jsonObject.get("password"));
//        c.setIdNum((String)jsonObject.get("idNum"));
//        c.setuName((String)jsonObject.get("uName"));
//        c.setpNum((String)jsonObject.get("pNum"));
//        c.setdINum((String)jsonObject.get("dINum"));
//        ownerDao.save(c);
//
//        return new MyJson("0", true);
        JSONObject jsonObject = JSON.parseObject(msg);
        Owner owner;
        try {
            Optional<Owner> o = ownerDao.findById((String)jsonObject.get("id"));
            if(o.isPresent()){
                JSONObject rtn = new JSONObject();
                rtn.put("msg", "existed id");
                return new MyJson("1", rtn);
            }
            owner = ownerDao.save(jsonObject.toJavaObject(Owner.class));
        } catch (Exception e){
            JSONObject rtn = new JSONObject();
            String errMsg = "lack of key";
            rtn.put("msg", errMsg);
            return new MyJson("1", rtn);
        }

        JSONObject rtn = new JSONObject();
        rtn.put("id", owner.getId());
        return new MyJson("0", rtn);
    }

    @RequestMapping("get")
    public MyJson one(@RequestBody() String msg){
        JSONObject jsonObject = JSON.parseObject(msg);

//        Optional<Owner> o = ownerDao.findById((String)jsonObject.get("id"));
//        if(!o.isPresent()){
//            return new MyJson("0", null);
//        }
//        Owner owner = o.get();
        Owner owner = jsonObject.toJavaObject(Owner.class);
        List<Owner> owners = ownerDao.findAll(Example.of(owner));

        return new MyJson("0", JSONArray.toJSON(owners));

    }

//    @RequestMapping("get-all")
//    public MyJson all(){
//        List<Owner> o = ownerDao.findAll();
//        if(o.isEmpty()){
//            return new MyJson("0", null);
//        }
//
//        return new MyJson("0", o);
//
//    }

    @RequestMapping("update")
    public MyJson update(@RequestBody() String msg){
        JSONObject jsonObject = JSON.parseObject(msg);
        if(!jsonObject.containsKey("id")){
            JSONObject rtn = new JSONObject();
            rtn.put("msg", "lack of id");
            return new MyJson("1", rtn);
        }

        try {
            Owner owner = ownerDao.save(jsonObject.toJavaObject(Owner.class));
            return new MyJson("0", JSONObject.toJSON(owner));
        } catch (Exception e){
            JSONObject rtn = new JSONObject();
            String errMsg = "lack of key";
            rtn.put("msg", errMsg);
            return new MyJson("1", rtn);
        }
    }
}
